<template>
    <div class="home">
        <div class="head">
            <div class="logo">
                <img src="../assets/logo.png" alt="logo" />
            </div>
            <div class="title">
                <h3>知识库自助查询</h3>
            </div>
            <div class="search">
                <div class="searchInput">
                    <img
                        class="searchIcon"
                        src="../assets/img/search.png"
                        alt=""
                    />
                    <input
                        type="text"
                        placeholder="电动车上牌"
                        v-model="searchText"
                    />
                    <button class="searchBtn" @click="search">搜索</button>
                </div>
                <div class="searchHot">
                    <p>热门问题：</p>
                    <button
                        @click="searchHot(item)"
                        v-for="(item, index) in searchHotData"
                        :key="index"
                    >
                        {{ item }}
                    </button>
                </div>
            </div>
        </div>
        <div class="content">
            <div class="tab">
                <div class="tabs" v-if="tabs.length > 0">
                    <div
                        class="tabsItem"
                        :class="tabsIndex == null ? 'activeTabsItem' : ''"
                        @click="changeTabs(null)"
                    >
                        所有文档
                    </div>
                    <div
                        class="tabsItem"
                        :class="tabsIndex == item.ID ? 'activeTabsItem' : ''"
                        v-for="(item, index) in tabs"
                        :key="index"
                        @click="changeTabs(item.ID)"
                    >
                        {{ item.NAME }}
                    </div>
                </div>
                <van-skeleton :row="2" class="skeleton" v-else/>
                <div v-if="data.length > 0">
                    <van-list
                        v-model:loading="loading"
                        :finished="finished"
                        finished-text="没有更多了"
                        :immediate-check="false"
                        @load="onLoad"
                    >
                        <div class="tabList">
                            <div
                                class="tabListItem"
                                v-for="(item, index) in data"
                                :key="index"
                                @click="changeList(item, index)"
                            >
                                <div class="tabListItemTitle">
                                    {{ item.title }}
                                </div>
                                <div class="tabListItemContent">
                                    {{ item.newcontent }}
                                </div>
                                <div class="tabListItemDate">
                                    <div class="tabListItemDateLeft">
                                        {{ item.type }}
                                    </div>
                                    <div class="tabListItemDateRight">
                                        录入时间：{{ item.updatetime }}
                                    </div>
                                </div>
                            </div>
                        </div>
                    </van-list>
                </div>
                <div v-else>
                    <van-skeleton
                        title
                        :row="2"
                        v-for="item in 8"
                        :key="item"
                        class="skeletonText"
                    />
                </div>
            </div>
        </div>
        <van-popup
            v-model:show="$store.state.popup"
            :style="{ width: '100%', height: '100vh' }"
            ><Article :articleData="articleData"
        /></van-popup>
    </div>
</template>

<script>
import request from "@/utils/request";
import qs from "qs";
import Article from "../views/Article.vue";
export default {
    components: {
        Article,
    },
    data() {
        return {
            tabs: [],
            searchHotData: ["电动车", "疫情防控", "社保缴纳"],
            searchText: null,
            tabsIndex: null, //tabs索引
            loading: true, //显示加载中
            finished: false, //显示没有更多了
            pageNum: 1, //页码
            pageSize: 10, //页数量
            data: [], //数据
            articleData: {}, //详情页数据
        };
    },
    methods: {
        //获取tabs
        getTabs() {
            let data = {
                type: "knowledge",
            };
            request
                .post(
                    "/12345epi/knowledge/queryKnowledgeType.json",
                    qs.stringify(data)
                )
                .then((res) => {
                    console.log(res.data);
                    if (res.data.response) {
                        this.tabs = res.data.data;
                        console.log(this.tabs);
                    }
                });
        },
        //tabsItem点击切换样式
        changeTabs(id) {
            console.log(id);
            this.searchText = null;
            this.tabsIndex = id;
            this.pageNum = 1;
            this.data = [];
            this.loading = true;
            this.getData();
        },
        //List点击事件
        changeList(item, index) {
            let data = {
                id: item.id,
            };
            request
                .post(
                    "/12345epi/knowledge/queryKnowledgeById.json",
                    qs.stringify(data)
                )
                .then((res) => {
                    console.log(res.data);
                    if (res.data.data) {
                        this.articleData = res.data.data;
                        this.$store.state.popup = true;
                    }
                });
        },
        //点击填入搜索信息
        searchHot(item) {
            this.searchText = item;
        },
        //获取首页数据
        getData() {
            this.data = [];
            this.finished = false;
            let data = {
                pageNum: this.pageNum,
                pageSize: this.pageSize,
                type: this.tabsIndex,
                secret: this.$store.state.secret,
            };
            this.requestData(data);
        },
        //搜索数据
        search() {
            this.finished = false;
            this.tabsIndex = '';
            this.data = [];
            this.pageNum = 1;
            let data = {
                pageNum: this.pageNum,
                pageSize: this.pageSize,
                keyword: this.searchText,
                secret: this.$store.state.secret,
            };
            this.requestData(data);
        },
        //列表加载
        onLoad() {
            this.pageNum++;
            let data = {
                pageNum: this.pageNum,
                pageSize: this.pageSize,
                type: this.tabsIndex,
                keyword: this.searchText,
                secret: this.$store.state.secret,
            };
            this.requestData(data);
        },
        //请求封装
        requestData(data) {
            request
                .post(
                    "/12345epi/knowledge/searchKnowledge.json",
                    qs.stringify(data)
                )
                .then((res) => {
                    if (res.data.data.length > 0) {
                        res.data.data.forEach((item) => {
                            item.newcontent = this.ToText(item.content);
                            this.data.push(item);
                        });
                        // 加载状态结束
                        this.loading = false;
                    } else {
                        this.empty = true;
                        this.finished = true;
                    }
                });
        },
        //html文本转换普通文本
        ToText(HTML) {
            let input = HTML;
            return input
                .replace(/<(style|script|iframe)[^>]*?>[\s\S]+?<\/\1\s*>/gi, "")
                .replace(/<[^>]+?>/g, "")
                .replace(/\s+/g, " ")
                .replace(/ /g, " ")
                .replace(/>/g, " ")
                .replace(/&nbsp;/g, " ");
        },
    },
    created() {
        this.getTabs();
        this.getData();
    },
};
</script>
<style lang="less" scoped>
.skeleton {
    width: 100%;
    height: 100%;
    padding: 10px;
    box-sizing: border-box;
    box-shadow: 0px 0px 4px #999;
    border-radius: 5px;
    background-color: #fff;
    /deep/.van-skeleton__row {
        height: 40px;
    }
}
.skeletonText {
    margin: 10px 0;
    padding: 10px;
    box-shadow: 0px 0px 4px #999;
}
.home {
    .head {
        width: 100%;
        height: 174px;
        background-image: url("../assets/img/ipad.png");
        background-position: center;
        background-size: cover;
        background-repeat: no-repeat;
        .logo {
            width: 67.5px;
            height: 33px;
            padding: 3px 0 0 5px;
            img {
                width: 100%;
                height: 100%;
            }
        }
        .title {
            width: 100%;
            text-align: center;
            h3 {
                font-size: 30px;
                font-weight: bold;
                color: #1576ea;
                letter-spacing: 4px;
                text-shadow: 0 0 5px #333;
            }
        }
        .search {
            margin-top: 10px;
            display: flex;
            flex-direction: column;
            align-items: center;
            .searchInput {
                width: 300px;
                position: relative;
                display: flex;
                align-items: center;
                justify-content: center;
                border-radius: 50px;
                overflow: hidden;
                box-sizing: border-box;
                .searchIcon {
                    width: 20px;
                    height: 16px;
                    position: absolute;
                    left: 10px;
                }
                input {
                    width: 100%;
                    border-radius: 50px;
                    border: 0;
                    padding: 10px 55px 10px 40px;
                }
                .searchBtn {
                    position: absolute;
                    right: 0;
                    width: 55px;
                    height: 100%;
                    font-size: 14px;
                    font-weight: bold;
                    color: #ffffff;
                    background-color: #1ab5ec;
                }
            }
            .searchHot {
                padding: 6px 0;
                width: 300px;
                display: flex;
                align-items: center;
                p {
                    font-size: 13px;
                    font-weight: 600;
                    color: #ffffff;
                }
                button {
                    font-size: 12px;
                    color: #333;
                    padding: 0.5px 8px;
                    margin-left: 4px;
                    background-color: #fff;
                    border: 1px solid #000;
                    border-radius: 5px;
                }
            }
        }
    }
    .content {
        width: 100%;
        height: 300px;
        position: relative;
        top: -20px;
        box-sizing: border-box;
        -moz-box-sizing: border-box;
        -webkit-box-sizing: border-box;
        .tab {
            padding: 0 10px;
            .tabs {
                display: flex;
                align-items: center;
                flex-wrap: wrap;
                background-color: #f6f6f6;
                border-radius: 5px;
                box-shadow: 0px 0px 4px #999;
                overflow: hidden;
                .tabsItem {
                    width: calc((100% - 50px) / 4);
                    margin-left: 10px;
                    margin-top: 10px;
                    padding: 10px 0;
                    text-align: center;
                    background-color: #fff;
                    box-shadow: 0px 0px 4px #999;
                    border-radius: 5px;
                    cursor: pointer;
                    &:nth-child(n + 5) {
                        margin-bottom: 10px;
                    }
                }
                .activeTabsItem {
                    background-color: #02adea;
                    color: #fff;
                }
            }
            .tabList {
                margin-top: 10px;
                .tabListItem {
                    padding: 10px;
                    margin-bottom: 10px;
                    background-color: #fff;
                    box-shadow: 0px 0px 4px #999;
                    .tabListItemTitle {
                        font-size: 14px;
                        font-weight: bold;
                        color: #1e1eff;
                    }
                    .tabListItemContent {
                        margin-top: 6px;
                        font-size: 13px;
                        color: #333333;
                        text-overflow: ellipsis;
                        white-space: nowrap;
                        overflow: hidden;
                    }
                    .tabListItemDate {
                        margin-top: 15px;
                        display: flex;
                        align-items: center;
                        justify-content: space-between;
                    }
                }
            }
        }
    }
}
</style>
